import {
    policy
} from "@/api/upload";
import {
    mapActions
} from "vuex";
import axios from "axios";
export default {
    components: {},
    data() {
        return {
            dataObj: {},
            num: 0,

        }
    },
    computed: {
        policInfo() {
            return this.$store.state.policInfo;
        }, //存签名信息
    },
    mounted() {
        if (!this.policInfo) {
            this.getPolicy();
        } else {
            let policInfo = JSON.parse(this.policInfo);
            let expire = policInfo.expire;
            let currentTime = new Date().getTime();
            if (expire < currentTime) {
                this.getPolicy();
                return;
            }
            this.dataObj = policInfo;
            if (this.seteditor) this.seteditor();

        }
    },
    watch: {
        policInfo(val) {
            if (!val) {
                this.getPolicy();
            } else {
                let policInfo = JSON.parse(val);
                let expire = policInfo.expire;
                let currentTime = new Date().getTime();
                if (expire < currentTime) {
                    this.getPolicy();
                    return;
                }
                this.dataObj = policInfo;
            }
        }, //存签名信息
    },
    methods: {
        ...mapActions(["setPolicInfo"]),
        //获取oss配置信息
        getPolicy(callback) {
            policy().then((res) => {
                if (res.code == 200 && res.data) {
                    this.setPolicInfo(JSON.stringify(res.data));
                    if (this.seteditor) this.seteditor();
                    if (typeof callback == "function") callback(res.data);
                }
            });
        },
        // 上传图片
        handelUpload(file, callback1, type) {
            const that = this;
            let {
                dir,
                policy,
                accessKey,
                signature,
                domain,
                callback,
                host,
                expire
            } = this.dataObj;
            //判断扩展名
            const tmpcnt = file.name.lastIndexOf(".");
            const exname = file.name.substring(tmpcnt + 1);
            if (!type) {
                const names = ["jpg", "jpeg", "png"];
                if (names.indexOf(exname) < 0) {
                    this.$message.error("不支持的格式!");
                    return;
                }
            }

            const fileName = new Date().getTime() + '' + parseInt(Math.random() * 1000000) + '.' + exname;
            var ossData = new FormData();
            ossData.append("key", dir + fileName);
            ossData.append("policy", policy);
            ossData.append("OSSAccessKeyId", accessKey);
            ossData.append("success_action_status", 200);
            ossData.append("callback", callback);
            ossData.append("signature", signature);
            ossData.append("file", file, file.name);
            axios
                .post(host, ossData)
                .then((res) => {
                    if (res.status == 200) {
                        // let { filename } = res.data.response || {};
                        let url = domain + "/" + dir + fileName;
                        // let url = domain + "/" + filename;
                        that.tempUrl = url;
                        callback1(url)
                    }


                })
                .catch((err) => {
                    // loading.close();
                    if (this.num > 10) {
                        return;
                    }
                    this.num += 1;
                    this.getPolicy((data) => {
                        this.dataObj = data;
                        this.handelUpload(file, callback)
                    });
                });
        },
    }
}